package com.ymttest.utils.compare;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

import org.json.JSONObject;

import com.ymttest.utils.FileUtil;

public class CompareManager {
	
	public static <T> void compareApiCall(String paramsFilePath,
			  Function<String[],Map<String,Object>> handleParams,
			  CallAPI oldApi,
			  CallAPI newApi,
			  Class<T> respnseBeanClass
			 ){
		compareApiCall(5,true,paramsFilePath,handleParams,oldApi,null,newApi,null,respnseBeanClass,null);
}
	
	
	public static <T> void compareApiCall(String paramsFilePath,
										  Function<String[],Map<String,Object>> handleParams,
										  CallAPI oldApi,
										  String oldTimeFormat,
										  CallAPI newApi,
										  String newTimeFormat,
										  Class<T> respnseBeanClass
										 ){
		compareApiCall(5,true,paramsFilePath,handleParams,oldApi,oldTimeFormat,newApi,newTimeFormat,respnseBeanClass,null);
	}

		
	
	
	
	
	
	public static <T> void compareApiCall(int threadNum,
									  boolean ignoreInfoPath,
									  String paramsFilePath,
									  Function<String[],Map<String,Object>> handleParams,
									  CallAPI oldApi,
									  String oldTimeFormat,
									  CallAPI newApi,
									  String newTimeFormat,
									  Class<T> respnseBeanClass,
									  BiFunction<JSONObject, T, T> beanHandlefunction){
		
		//config
		LogPathConf logPathConf = new LogPathConf(ignoreInfoPath);
		LoggerInterface logger = new LoggerHelper(logPathConf);
		CompareHelper compareHelper = new CompareHelper(logger, threadNum);
		
		//prepare request data
		List<String> paramsLineList = FileUtil.readFileAsLine(paramsFilePath);
		List<Map<String,Object>> paramMapList = new ArrayList<>();
		paramsLineList.forEach(paramLine->{
			String[] paramLineAry = paramLine.split(",");
			paramMapList.add(handleParams.apply(paramLineAry));
		});
		compareHelper.setCompareApiData(oldApi, oldTimeFormat,
				newApi, newTimeFormat, paramMapList,
				respnseBeanClass,beanHandlefunction);
		
		compareHelper.startCompare();
	}
	
	
	
}
